import {
  getAllCPUData,
  getAllGPUData,
  getAllMainboardData,
  getAllMemoryData,
  getAllSSDData,
  getAllHDDData,
} from "@/server/assembly/index";

interface Component {
  _id: string;
  price: number;
  score: number;
}

interface Components {
  [key: string]: Component[];
}

interface Configuration {
  [key: string]: Component;
}

interface BestConfiguration {
  configuration: Configuration;
  totalPrice: number;
  totalRating: number;
}
function findBestConfiguration(
  targetPrice: number,
  components: Components
): BestConfiguration {
  let bestConfiguration: BestConfiguration = {
    configuration: {},
    totalPrice: 0,
    totalRating: 0,
  };
  let bestRating = 0;

  // 对每种组件按价格排序
  const sortedComponents: Components = {};
  for (const key in components) {
    sortedComponents[key] = components[key].sort((a, b) => a.price - b.price);
  }

  // 初始化当前组合和总价格，确保每种组件恰好包含一个
  const currentConfiguration: Configuration = {};
  let totalPrice = 0;

  for (const key in sortedComponents) {
    if (sortedComponents[key].length > 0) {
      currentConfiguration[key] = sortedComponents[key][0];
      totalPrice += sortedComponents[key][0].price; // 更新totalPrice的值
    } else {
      // 如果某种组件没有可选项，则无法达到目标价格，直接返回空配置
      return bestConfiguration;
    }
  }

  // 随机选择其他组件直到总价格超过目标价格
  for (const key in sortedComponents) {
    while (totalPrice <= targetPrice && sortedComponents[key].length > 1) {
      const randomIndex =
        Math.floor(Math.random() * (sortedComponents[key].length - 1)) + 1;
      const componentToAdd = sortedComponents[key][randomIndex];
      if (totalPrice + componentToAdd.price <= targetPrice) {
        currentConfiguration[key] = componentToAdd;
        totalPrice += componentToAdd.price;
      } else {
        break;
      }
    }
  }

  // 计算当前组合的综合评分
  let totalRating = 0;
  for (const key in currentConfiguration) {
    totalRating += currentConfiguration[key].score;
  }
  totalRating /= Object.keys(currentConfiguration).length;

  // 如果当前组合的综合评分更高，或者评分相同但总价格更接近目标价格，则更新最佳组合
  if (
    totalRating > bestRating ||
    (totalRating === bestRating &&
      Math.abs(totalPrice - targetPrice) <
        Math.abs(bestConfiguration.totalPrice - targetPrice))
  ) {
    bestConfiguration = {
      configuration: currentConfiguration,
      totalPrice: totalPrice,
      totalRating: totalRating,
    };
  }
  console.log(bestConfiguration);

  return bestConfiguration;
}

export const getInfo = async (price: number) => {
  try {
    const [cpuData, gpuData, mainboardData, memoryData, ssdData, hddData] =
      await Promise.all([
        getAllCPUData(),
        getAllGPUData(),
        getAllMainboardData(),
        getAllMemoryData(),
        getAllSSDData(),
        getAllHDDData(),
      ]);

    const ass = {
      CPU: cpuData.data,
      GPU: gpuData.data,
      Center: mainboardData.data,
      Memory: memoryData.data,
      SSD: ssdData.data,
      HDD: hddData.data,
    };
    // console.log(ass);
    const assobj = findBestConfiguration(price, ass);

    return assobj;
  } catch (error) {
    console.log(error);
  }
};
